<template>
  <div class="live-goods">
    <div class="content">
      <div class="top-wrapper">
        <div class="top">
          <div class="title-wrapper">
            <div class="title">直播间商品</div>
          </div>
        </div>
        <el-form ref="form" :model="form" label-width="100px">
          <el-form-item label="直播间:">
            <p>{{ goodsInfo.name }}</p>
          </el-form-item>
          <el-form-item label="直播时间:">
            <p>{{ goodsInfo.start_time }}至{{ goodsInfo.end_time }}</p>
          </el-form-item>
        </el-form>
      </div>
      <div class="bottom">
        <div class="bottom-top">
          <p>已导入 {{ goodsInfo.goods.length }} 件商品，本场直播还可导入 {{ total - goodsInfo.goods.length }} 件</p>
          <el-button type="primary" @click="importFromGoodsStock">从商品库导入商品</el-button>
        </div>
        <div class="bottom-table">
          <el-table :data="goodsInfo.goods" style="width: 100%">
            <el-table-column label="序号" width="100">
              <template slot-scope="scope">
                <div>{{ scope.$index + 1 }}</div>
              </template>
            </el-table-column>
            <el-table-column label="商品信息" width>
              <template slot-scope="scope">
                <div class="d-flex-c">
                  <el-avatar :size="50" :src="scope.row.cover_img" class="border" />
                  <div class="d-flex-colomn">
                    <span style="width: 150px">{{ scope.row.name }}</span>
                  </div>
                </div>
              </template>
            </el-table-column>
            <el-table-column label="价格" width="150">
              <template slot-scope="scope">
                <div>{{ scope.row.price + '元' }}</div>
              </template>
            </el-table-column>
            <el-table-column prop="url" label="商品链接" width="300" />
            <el-table-column label="操作" width="250">
              <template>
                <span class="blue mr-100" @click="stickyClick">置顶</span>
                <span class="red">删除</span>
              </template>
            </el-table-column>
          </el-table>
        </div>
      </div>
    </div>
    <el-dialog
      title="选择商品"
      :visible.sync="dialogVisible"
      class="goodsDialog"
      :before-close="handleClose"
    >
      <span slot="title" class="d-flex">
        <p class="fz-18 fwb mr-20">选择商品</p>
        <p class="fz-14 color-9">已选 {{ multipleList.length }} 件商品，本场直播还可选 {{ total - multipleList.length }} 件</p>
      </span>
      <div class="dialog-content">
        <el-table
          ref="multipleTable"
          :data="list"
          tooltip-effect="dark"
          style="width: 100%"
          @selection-change="handleSelectionChange"
        >
          <el-table-column type="selection" width="55" />
          <el-table-column label="图片">
            <template slot-scope="scope">
              <div>
                <el-avatar shape="square" :size="60" :src="scope.row.cover_img_url" />
              </div>
            </template>
          </el-table-column>
          <el-table-column label="名称" prop="name" />
          <el-table-column label="价格">
            <template slot-scope="scope"><div>{{ scope.row.priceType === 1 ?'一口价:' + scope.row.price : scope.row.priceType === 2 ? '价格区间:' + (price + '——' + price2) : '原价:' + scope.row.price }}元</div>
              <p v-if="scope.row.priceType===3">现价:  {{ scope.row.price2 }}元</p></template>
          </el-table-column>
        </el-table>
        <Pagination class="pagination" :total="total" :cur-page="currentPage" :page-size="4" @nextPage="handleNextPage" />
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="addGoods">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>
<script>
import { getLiveGoodsList, importGoodsToLiveRome } from '@/api/live'
import Pagination from '@/common/pagination'
const ERR_OK = 200
export default {
  components: {
    Pagination
  },
  data() {
    return {
      total: 0,
      currentPage: 1,
      goodsInfo: {},
      dialogVisible: false,
      form: {},
      list: [],
      multipleList: []
    }
  },
  mounted() {
    this.$nextTick(() => {
      this.goodsInfo = JSON.parse(decodeURIComponent(this.$route.query.goodsInfo))
    })
  },
  created() {
    this._getLiveGoodsList({ status: 2, page: this.currentPage })
  },
  methods: {
    addGoods() {
      if (!this.multipleList.length) {
        this.$message({
          message: '请选择商品',
          showClose: true,
          type: 'success'
        })
      } else {
        const good_ids = []
        this.multipleList.forEach((item, index) => {
          good_ids.push(item.goodsId)
        })
        console.log(this.goodsInfo)
        this._importGoodsToLiveRome({ room_id: this.goodsInfo.roomid, good_ids })
          .then(() => {
            this.dialogVisible = false
            this.$router.push({ path: '/live/index' })
          })
      }
    },
    handleNextPage(page) {
      this.currentPage = page
      this._getLiveGoodsList({ status: 2, page: this.currentPage })
    },
    handleSelectionChange(val) {
      console.log(val, 'val')
      this.multipleList = val
    },
    stickyClick() {},
    importFromGoodsStock() {
      this.dialogVisible = true
    },
    handleClose() {
      this.dialogVisible = false
    },
    _getLiveGoodsList({ status, page }) {
      getLiveGoodsList({ status, page })
        .then(res => {
          if (res.code === ERR_OK) {
            this.total = res.data.total
            this.list = res.data.data
          }
        })
        .catch(err => {
          console.log(err)
        })
    },
    _importGoodsToLiveRome({ room_id, good_ids }) {
      return importGoodsToLiveRome({ room_id, good_ids })
        .then(res => {
          if (res.code === ERR_OK) {
            console.log(res)
            this.$message({
              message: '导入成功',
              showClose: true,
              type: 'success'
            })
            Promise.resolve()
          }
        })
        .catch(err => {
          console.log(err)
        })
    }
  }
}
</script>
<style lang="scss" scoped>
@import '@/styles/el-pagination.scss';
@import "@/styles/el-table.scss";
.goodsDialog {
  min-width: 1200px;
}
p {
  margin: 0;
}
.top-wrapper {
  border-bottom: 1px solid #eee;
}

.live-goods {
  padding: 0 20px;
  box-sizing: border-box;
  min-height: 110vh;
  .top {
    width: 100%;
    box-sizing: border-box;
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 10px 0;
    .title-wrapper {
      display: flex;
      align-content: center;
      .title {
        height: 44px;
        line-height: 44px;
        color: rgba(68, 68, 68, 1);
        font-size: 18px;
        font-family: Roboto;
        font-weight: bold;
        cursor: pointer;
      }
    }
  }
  .bottom {
    margin-top: 25px;
    .bottom-top {
      display: flex;
      justify-content: space-between;
      align-items: center;
      margin-bottom: 30px;
      p {
        color: rgba(153, 153, 153, 1);
        font-size: 14px;
        line-height: 22px;
        text-align: left;
        font-family: SourceHanSansSC-regular;
      }
    }
  }
}
.d-flex-colomn {
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
}
.d-flex {
  display: flex;
  align-items: center;
}
.d-flex-c {
  display: flex;
  justify-content: center;
  align-items: center;
}

.blue {
  color: #409eff;
  cursor: pointer;
}
.red {
  color: #f56c6c;
  cursor: pointer;
}
.mr-100 {
  margin-right: 100px;
}
.mr-20 {
  margin-right: 20px;
}
.fz-18 {
  font-size: 18px;
}
.fz-14 {
  font-size: 14px;
}
.fwb {
  font-weight: bold;
}
.color-9 {
  color: #9999;
}
</style>
